<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Blog\Article;
use App\Models\Blog\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CommentService
{

    use Service;

    /**
     * 评论首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function CommentIndexService( $request )
    {
        $with     = [
            'article'    => function ( $query ) {
                $query->select('id', 'title');
            },
            'user'       => function ( $query ) {
                $query->select('id', 'name');
            },
            'clientName' => function ( $query ) {
                $query->select('id', 'name');
            },
        ];
        $where    = function ( $query ) use ( $request ) {
            if ( $request->filled('article_id') ) $query->where('article_id', $request->input('article_id'));
        };
        $whereHas = function ( $query ) use ( $request ) {
            $query->where('title', 'like', "%" . $request->input('title') . "%");
        };
        $select   = [
            'id', 'user_id', 'article_id', 'client_name', 'content', 'filter', 'filter_content',
            'parent_id', 'is_show',
        ];
        $comment  = Comment::GetModel($where, $with, 'article', $whereHas, $select);
        $comment->appends([
            'title' => $request->input('title'),
        ])->render();
        return view('blog.comment.index', compact('comment', 'request'));
    }

    /**
     * 博客文章评论删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CommentDestroyService( $id )
    {
        DB::transaction(function () use ( $id ) {
            $where   = function ( $query ) use ( $id ) {
                $query->where('id', $id);
            };
            $comment = Comment::FindModel($where);
            Article::DecrementModel([ 'article_id' => $comment->article_id ], 'comment');
            $comment->delete();
        });
        return back()->with('success', $this->getErrorCode()::message(204));
    }

    /**
     * 批量删除文章评论
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CommentDeleteService( $request )
    {
        DB::transaction(function () use ( $request ) {
            foreach ( $request->input('id') as $value ) {
                $where   = function ( $query ) use ( $value ) {
                    $query->where('id', $value);
                };
                $comment = Comment::FindModel($where);
                Article::DecrementModel([ 'article_id' => $comment->article_id ], 'comment');
            }
            Comment::DestroyModel($request->input('id'));
        });
        return data();
    }


    /**
     * 修改 评论状态
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CommentStatusService( $id )
    {
        $comment = Comment::FindModel([ 'id' => $id ], [ 'id', 'is_show' ]);
        if ( $comment->is_show == 1 ) $is_show = 2;
        else $is_show = 1;
        $comment->is_show = $is_show;
        $comment->save();
        return back()->with('success', $this->getErrorCode()::message(210));
    }
}
